package com.you.pattern.observer.demo2;


import java.util.Vector;

public class Observable {

    Vector<Observer> observers = new Vector();

    boolean change = false;

    public void notifyObservers(Object object) {
        synchronized (this) {
            if (!change) { //如果没有事件变更操作
                return;
            }
            //通知观察者触发更新事件
            for (Observer observer : observers) {
                observer.update(this, object);
            }
        }
    }

    public synchronized void setChange(boolean change) {
        this.change = change;
    }

    public synchronized void addObservers(Observer observer) {
        observers.add(observer);
    }

    public synchronized void removeObservers(Observer observer) {
        observers.remove(observer);
    }
}
